#include "SWM320.h"
#include "crc.h"
#include "uart_process.h"
#include "sys.h"

void serial_init(void)
{
    UART_InitStructure UART_initStruct;

    PORT_Init(PORTC, PIN4, FUNMUX0_UART0_RXD, 1); //GPIOC.4配置为UART0输入引脚
    PORT_Init(PORTC, PIN5, FUNMUX1_UART0_TXD, 0); //GPIOC.5配置为UART0输出引脚

    UART_initStruct.Baudrate = 115200;
    UART_initStruct.DataBits = UART_DATA_8BIT;
    UART_initStruct.Parity = UART_PARITY_NONE;
    UART_initStruct.StopBits = UART_STOP_1BIT;
    UART_initStruct.RXThreshold = 3;
    UART_initStruct.RXThresholdIEn = 1;
    UART_initStruct.TXThreshold = 3;
    UART_initStruct.TXThresholdIEn = 0;
    UART_initStruct.TimeoutTime = 10;
    UART_initStruct.TimeoutIEn = 1;
    UART_Init(UART0, &UART_initStruct);
    UART_Open(UART0);

    FIFO_Init(&uartFifo, FIFO_BUF_LENGTH);
}

u8 send_buf[1024];

void send_Serial(u8 *buf, u16 length)
{
    for (int i = 0; i < length; i++)
    {
        UART_WriteByte(UART0, buf[i]);
        while (UART_IsTXBusy(UART0)) {};
    }
}

void send_Serial_CMD(u8 cmd)
{
    u16 len = 0;
    u32 crc;

    send_buf[len++] = 0x55;
    send_buf[len++] = 0x55;
    send_buf[len++] = 0;   //长度高位
    send_buf[len++] = 0;   //长度低位
    send_buf[len++] = cmd; //CMD

    send_buf[2] = (u8)(len >> 8);
    send_buf[3] = (u8)(len >> 0);

    crc = 0;
    crc = calc_crc32(crc, &send_buf[4], len - 4);

    send_buf[len++] = (u8)(crc >> 24);
    send_buf[len++] = (u8)(crc >> 16);
    send_buf[len++] = (u8)(crc >> 8);
    send_buf[len++] = (u8)(crc >> 0);

    for (int i = 0; i < len; i++)
    {
        UART_WriteByte(UART0, send_buf[i]);
        while (UART_IsTXBusy(UART0)) {};
    }
}

/*数据格式*/
/*头码55 55，长度包含除头码之外的所有字节*/
//  头码    |  长度  |  命令  |  数据  |  校验
//  55 55      00 03     01       01
void send_Serial_Data(u8 cmd, u8 *buf, u16 length)
{
    u16 len = 0;
    u32 crc;

    send_buf[len++] = 0x55;
    send_buf[len++] = 0x55;
    send_buf[len++] = 0;   //长度高位
    send_buf[len++] = 0;   //长度低位
    send_buf[len++] = cmd; //CMD

    for (int i = 0; i < length; i++)
    {
        send_buf[len++] = buf[i];
    }
    send_buf[2] = (u8)(len >> 8);
    send_buf[3] = (u8)(len >> 0);

    crc = 0;
    crc = calc_crc32(crc, &send_buf[4], len - 4);

    send_buf[len++] = (u8)(crc >> 24);
    send_buf[len++] = (u8)(crc >> 16);
    send_buf[len++] = (u8)(crc >> 8);
    send_buf[len++] = (u8)(crc >> 0);

    for (int i = 0; i < len; i++)
    {
        UART_WriteByte(UART0, send_buf[i]);
        while (UART_IsTXBusy(UART0)) {};
    }
}

void UART0_Handler(void)
{
    uint32_t chr;

    //    if (UART_INTRXThresholdStat(UART0))
    //    {
    //        while ((UART0->FIFO & UART_FIFO_RXLVL_Msk) > 1)
    //        {
    //            if (UART_ReadByte(UART0, &chr) == 0)
    //            {
    //                //UART_RXBuffer[UART_RXIndex++] = chr;
    //                uartFifo->buffer[FIFO_BUF_LENGTH - uartFifo->CNDTR] = chr;
    //                uartFifo->CNDTR--;
    //                if (uartFifo->CNDTR == 0)
    //                {
    //                    uartFifo->CNDTR = FIFO_BUF_LENGTH;
    //                }
    //            }
    //        }
    //    }

    if (UART_INTRXThresholdStat(UART0))
    {
        while ((UART0->FIFO & UART_FIFO_RXLVL_Msk) > 1)
        {
            if (UART_ReadByte(UART0, &chr) == 0)
            {
                uartFifo->buffer[FIFO_BUF_LENGTH - uartFifo->CNDTR] = chr;
                uartFifo->CNDTR--;
                if (uartFifo->CNDTR == 0)
                {
                    uartFifo->CNDTR = FIFO_BUF_LENGTH;
                }
            }
        }
    }
    else if (UART_INTTimeoutStat(UART0))
    {
        while (UART_IsRXFIFOEmpty(UART0) == 0)
        {
            if (UART_ReadByte(UART0, &chr) == 0)
            {
                uartFifo->buffer[FIFO_BUF_LENGTH - uartFifo->CNDTR] = chr;
                uartFifo->CNDTR--;
                if (uartFifo->CNDTR == 0)
                {
                    uartFifo->CNDTR = FIFO_BUF_LENGTH;
                }
            }
        }
        //rx_ok_flag = 1;
    }
}
